package org.jeecg.common.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

public class StyleHelper {
    public static void cloneRowStyle(Workbook workbook, Row sourceRow, Row targetRow) {
        if (sourceRow == null) return;

        // 复制行高
        targetRow.setHeight(sourceRow.getHeight());

        // 遍历所有单元格
        for (Cell srcCell : sourceRow) {
//            Cell destCell = targetRow.createCell(srcCell.getColumnIndex());
            Cell destCell = targetRow.getCell(srcCell.getColumnIndex()) != null ? targetRow.getCell(srcCell.getColumnIndex()) : targetRow.createCell(srcCell.getColumnIndex());
            cloneCellStyle(workbook, srcCell, destCell);
        }
    }

    private static void cloneCellStyle(Workbook workbook, Cell srcCell, Cell destCell) {
        CellStyle newStyle = workbook.createCellStyle();
        newStyle.cloneStyleFrom(srcCell.getCellStyle());
        destCell.setCellStyle(newStyle);

        // 特殊类型处理
//        switch (srcCell.getCellType()) {
//            case FORMULA:
//                destCell.setCellFormula(srcCell.getCellFormula());
//                break;
//            case NUMERIC:
//                destCell.setCellValue(srcCell.getNumericCellValue());
//                break;
//            case STRING:
//                destCell.setCellValue(srcCell.getStringCellValue());
//                break;
//            case BOOLEAN:
//                destCell.setCellValue(srcCell.getBooleanCellValue());
//                break;
//        }
    }
}
